package it.slkfarm.android;

import it.slkfarm.bean.Crop;
import it.slkfarm.log.LogData;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class CropDetails extends Base implements OnClickListener
{
	/**
	 * @uml.property  name="max"
	 * @uml.associationEnd  
	 */
	private TextView max;
	/**
	 * @uml.property  name="current"
	 * @uml.associationEnd  
	 */
	private TextView current;
	/**
	 * @uml.property  name="percent"
	 * @uml.associationEnd  
	 */
	private TextView percent;
	/**
	 * @uml.property  name="size"
	 * @uml.associationEnd  
	 */
	private TextView size;
	/**
	 * @uml.property  name="weight"
	 * @uml.associationEnd  
	 */
	private TextView weight;
	/**
	 * @uml.property  name="color"
	 * @uml.associationEnd  
	 */
	private TextView color;
	/**
	 * @uml.property  name="title"
	 * @uml.associationEnd  
	 */
	private TextView title;
	/**
	 * @uml.property  name="close"
	 * @uml.associationEnd  
	 */
	private Button close;
	/**
	 * @uml.property  name="currentImage"
	 * @uml.associationEnd  
	 */
	private ImageView currentImage;
	/**
	 * @uml.property  name="crop"
	 * @uml.associationEnd  
	 */
	private Crop crop;
	/**
	 * @uml.property  name="tf"
	 * @uml.associationEnd  
	 */
	private Typeface tf;
	/**
	 * @uml.property  name="position"
	 */
	private int position;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{	
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setLanguage(dbAdapter.getSetLanguage());
		tf = getTypeface();
		setContentView(R.layout.crop_details);
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		crop = (Crop) getIntent().getSerializableExtra(getPackageName()+".crop");
		position = getIntent().getIntExtra(getPackageName() + ".position", 0);
		title = (TextView) findViewById(R.id.tvDetails);
		title.setTypeface(tf);
		max = (TextView) findViewById(R.id.tvDetailsMax);
		max.setTypeface(tf);
		current = (TextView) findViewById(R.id.tvDetailsCurrent);
		current.setTypeface(tf);
		percent = (TextView) findViewById(R.id.tvDetailsPercent);
		percent.setTypeface(tf);
		size = (TextView) findViewById(R.id.tvDetailsSize);
		size.setTypeface(tf);
		weight = (TextView) findViewById(R.id.tvDetailsWeight);
		weight.setTypeface(tf);
		color = (TextView) findViewById(R.id.tvDetailsColor);
		color.setTypeface(tf);
		
		close = (Button) findViewById(R.id.btnClose);
		close.setTypeface(tf);
		
		close.setOnClickListener(this);
		
		setDoubleColor(max, String.valueOf(crop.getMaxProduction())+"q");
		setDoubleColor(current, String.valueOf(crop.getCurrentProduction())+"q");
		setDoubleColor(percent, String.valueOf(crop.getPercentageProduction())+"%");
		setDoubleColor(size, crop.getSize());
		setDoubleColor(weight, crop.getWeight());
		setDoubleColor(color, crop.getColor());
		
		currentImage = (ImageView) findViewById(R.id.imgCurrent);
		currentImage.setImageResource(setCurrentImage(crop.getPercentageProduction()));
	}

	public void onClick(View v) 
	{
		LogData log = new LogData("CropDetails","Button Close","Close details");
		logger.appendLog(log);
		finish();
	}	
	
	public void setDoubleColor(TextView t, String a)
	{
		String app = t.getText().toString();
		SpannableString span = new SpannableString(app + " " + a);
		span.setSpan(new ForegroundColorSpan(Color.parseColor("#43520B")), app.length()+1, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		t.setText(span, BufferType.SPANNABLE);
	}
	
	public int setCurrentImage(Double value)
	{
		int res = 0;
		if (value <= 33)
			res = R.drawable.under;
		else if (value>=34 && value<=67)
			res = R.drawable.normal;
		else if (value>=68)
			res = R.drawable.over;
		return res;
	}
}
